#include "knowledgebase.h"

namespace IAlib
{
    KnowledgeBase::KnowledgeBase()
    {
    }

    KnowledgeBase::KnowledgeBase(const KnowledgeBase &knowledge)
    {
        m_environmental_knowledge = knowledge.m_environmental_knowledge;
        m_social_knowledge = knowledge.m_social_knowledge;
        m_means_knowledge = knowledge.m_means_knowledge;
    }

    void KnowledgeBase::addEnvironmentalKnowledge(const EnvironmentalKnowledge &kw)
    {
        m_environmental_knowledge.append(kw);
    }

    void KnowledgeBase::addMeansKnowledge(const MeansKnowledge &kw)
    {
        bool exist = false;
        int i = 0;
        for ( i = 0; i < m_means_knowledge.size(); i++)
        {
            if (m_means_knowledge[i].getMeansID() == kw.getMeansID())
            {
                exist = true;
                break;
            }
        }
        if (!exist)
        {
            m_means_knowledge.append(kw);
        }
        else
        {
            if (kw.getLastCheck() > m_means_knowledge[i].getLastCheck())
            {
                m_means_knowledge[i] = kw;
            }
        }
    }

    void KnowledgeBase::addSocialKnowledge(const SocialKnowledge &kw)
    {
        bool exist = false;
        int i = 0;
        for ( i = 0; i < m_social_knowledge.size(); i++)
        {
            if (m_social_knowledge[i].getActorID() == kw.getActorID())
            {
                exist = true;
                break;
            }
        }
        if (!exist)
        {
            m_social_knowledge.append(kw);
        }
        else
        {
            if (kw.getLastCheck() > m_social_knowledge[i].getLastCheck())
            {
                m_social_knowledge[i] = kw;
            }
        }
    }

    void KnowledgeBase::removeEnvironmentalKnowledge(const QString &id)
    {
        for (int i = 0; i < m_environmental_knowledge.size(); i++)
        {
            if (m_environmental_knowledge[i].getID() == id)
            {
                m_environmental_knowledge.removeAt(i);
                break;
            }
        }
    }

    void KnowledgeBase::removeSocialKnowledge(const QString &id)
    {
        for (int i = 0; i < m_social_knowledge.size(); i++)
        {
            if (m_social_knowledge[i].getActorID() == id)
            {
                m_social_knowledge.removeAt(i);
                break;
            }
        }
    }

    void KnowledgeBase::removeMeansKnowledge(const QString &id)
    {
        for (int i = 0; i < m_means_knowledge.size(); i++)
        {
            if (m_means_knowledge[i].getMeansID() == id)
            {
                m_means_knowledge.removeAt(i);
                break;
            }
        }
    }
}
